<?php
namespace app\user\controller;
use app\common\controller\UserBase;
use app\user\model\User;
use app\user\model\UserAuthGroup;

class Staff extends UserBase
{
    protected $noAuth = ['index','edit_user', 'edit_info','edit_password','logout'];
    
    protected function _initialize()
    {
        parent::_initialize();

    }
    //获取用户列表
    public function index()
    {
        //用户列表
        $list = User::with(['userAuthGroup','shop'])->order('update_time desc,id desc')->paginate();
        $data = ['list' => $list];
        return $this->fetch('index',$data);
    }
    //新增
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['shop_id'] = ShopId();
            $param['status'] = '1';
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'staff');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            User::create($param);
            insert_user_log('添加了用户:'.$param['name']);
            $this->success('操作成功');
        }
        return $this->fetch('save',['userAuthGroup' => UserAuthGroup::list_to_level()]);
    }
    //编辑
    public function edit($id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0'){
                try{
                    $this->validate($param, 'staff');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            if($param['id'] != '1'){
                User::update($param,['id'=>$param['id']]);
            }
            insert_user_log('修改了账户');
            $this->success('操作成功');
        }
        $data = User::where('id',$id)->find();
        return $this->fetch('save',['data'=>$data,'userAuthGroup'=>UserAuthGroup::select()]);
    }
    //编辑
    public function edit_info()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            User::where('id', UserId())->update(['name'=>$param['name']]);
            insert_user_log('修改了个人信息');
            $this->success('操作成功');
        }
        return $this->fetch('edit_info',['data'=>User::where('id', UserId())->find()]);
    }
    //编辑
    public function edit_password()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['password'] = md5($param['password']);
            $param['new_password'] = md5($param['new_password']);
            //验证密码
            $checkpassword = User::where(['id'=>UserId(),'password'=>$param['password']])->find();
            if(!$checkpassword){
                $this->error('原密码错误');
            }
            if(UserId() != '1'){
                User::where('id', UserId())->update(['password'=>$param['new_password']]);
            }
            insert_user_log('修改了账户密码');
            $this->success('操作成功');
        }
        return $this->fetch('edit_password');
    }
    //删除
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if($param['id'] == UserId() or UserId() == '1'){
                $this->error('您不能删除自己！');
            }
            insert_user_log('删除了用户');
            User::destroy($param['id']);
            $this->success('删除成功');
        }
        
    }
 
    
    
    
}
